
var callbacks=[]
var microTimerFunc;

var pending = false;

function flushCallbacks() {

    pending = false;
    var copies = callbacks.slice(0);
    callbacks.length = 0;

    for (var i = 0; i < copies.length; i++) {
        copies[i]();
    }

}

if (typeof Promise !== 'undefined') {

    var p = Promise.resolve();

    microTimerFunc = function() {
        console.log('microTimerFunc--->');
        p.then(flushCallbacks);
    };

} else {
    microTimerFunc = function() {
        setTimeout(flushCallbacks, 0);
    };;
}

function nextTick(cb, ctx) {
    callbacks.push(function() {
        cb && cb.call(ctx);
    });
    if (!pending) {
        pending = true;
        microTimerFunc();
    }
}

nextTick(()=>{ console.log(1)})
nextTick(()=>{console.log(2)})

